package com.myk.game.gobangchess.rooms.absrooms;

import com.myk.game.gobangchess.*;
import com.myk.game.gobangchess.constants.ColorCost;
import com.myk.game.gobangchess.signs.GroundSign;

import java.util.ArrayList;
import java.util.List;

/**
 * 一二二二落子制棋的抽象类
 */
public abstract class AbsOneTwoTwoTwoStepRoom extends Room {

    protected int[] firstPoint; //长度为3的数组，row，col，value
    protected int[] secondPoint; //长度为3的数组，row，col，value
    protected List<Sign> tempPointSigns = new ArrayList<>(); //记录最新落点的标记

    public AbsOneTwoTwoTwoStepRoom(String roomName, String roomIntroduce, String chessName,int[][] curGrids, boolean isSupportPersonal,boolean isSaveHistoryToFile,String ... modes) {
        super(roomName, roomIntroduce, chessName,curGrids, isSupportPersonal,isSaveHistoryToFile,modes);
    }

    @Override
    public void resetGrids() {
        super.resetGrids();
        firstPoint = null;
        secondPoint = null;
        tempPointSigns.clear();
    }

    @Override
    public void doRule(int row, int col, int value) {
        if (curGrids[row][col] != 0) {
            return;
        }
        curGrids[row][col] = value;
        if (step == 0){
            stepAdd();;
            firstPoint = new int[]{row,col,value};
            updateTempPointSigns();
            noticeChangeMove();
        }
        else {
            if (firstPoint == null){
                WinState winState = checkWin(row,col);
                if (winState != null) {
                    doOver(winState.winMode,winState.winMsg);
                }
                else {
                    firstPoint = new int[]{row,col,value};
                    updateTempPointSigns();
                    noticeAllRefreshGameInfo();
                }
            }
            else {
                secondPoint = new int[]{row,col,value};
                updateTempPointSigns();
                stepAdd();
                WinState winState = checkWin(row,col);
                if (winState != null) {
                    doOver(winState.winMode,winState.winMsg);
                }
                else {
                    noticeChangeMove();
                }
            }
        }
    }

    protected void updateTempPointSigns(){
        tempPointSigns.clear();
        //新落子提示
        if (firstPoint != null && secondPoint != null) {
            tempPointSigns.add(new GroundSign(getIndex(firstPoint[0], firstPoint[1]), "#00000000", ColorCost.NEW_PUT_POINT, 1.0f, 0, 0));
            tempPointSigns.add(new GroundSign(getIndex(secondPoint[0], secondPoint[1]), "#00000000", ColorCost.NEW_PUT_POINT, 1.0f, 0, 0));
        }
        else if (firstPoint != null) {
            tempPointSigns.add(new GroundSign(getIndex(firstPoint[0], firstPoint[1]), "#00000000", ColorCost.NEW_PUT_POINT, 1.0f, 0, 0));
        }
    }

    //切换行动方
    protected void noticeChangeMove() {
        if (cur == 1) {
            noticeWhiteMove();
        }
        else if (cur == 2) {
            noticeBlackMove();
        }
        firstPoint = null;
        secondPoint = null;
    }

    protected abstract WinState checkWin(int row, int col);

    protected int getJudgeLength(int x, int y, int dx, int dy) {
        int value = curGrids[x][y];
        int length = 0;
        while (isInGrids(x, y)) {
            if (curGrids[x][y] != value) {
                return length;
            }
            length++;
            x += dx;
            y += dy;
        }
        return length;
    }

    // 判断位置在棋盘上
    protected boolean isInGrids(int row, int col) {
        return row >= 0 && row <= curGrids.length - 1 && col >= 0 && col <= curGrids[0].length - 1;
    }

    public static class WinState{
        public int winMode;
        public String winMsg;

        public WinState(int winMode, String winMsg) {
            this.winMode = winMode;
            this.winMsg = winMsg;
        }

    }
}
